package com.springboot.framework.controller;

import com.springboot.framework.annotation.ACS;
import com.springboot.framework.controller.request.SellerInsertRequestBean;
import com.springboot.framework.controller.request.SellerUpdateBean;
import com.springboot.framework.controller.request.StatusUpdateBean;
import com.springboot.framework.enums.Errors;
import com.springboot.framework.enums.StatusEnum;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.SellerModel;
import com.springboot.framework.model.UserModel;
import com.springboot.framework.service.SellerService;
import com.springboot.framework.utils.ResponseVoUtil;
import com.springboot.framework.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author SWF
 * @Date 2019/6/8 23:27
 **/
@Api(description = "【商家】用户")
@RestController
@RequestMapping("/seller")
public class SellerController extends BaseController {

    @Resource
    private SellerService sellerService;

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "卖家登录",notes = "卖家登录")
    @GetMapping("/login")
    public ResponseVO<SellerModel> login(String openId, HttpServletRequest request){
        if(openId == null){
            return null;
        }
        SellerModel sellerModel = sellerService.get(openId);
        if(sellerModel == null){
            throw new BusinessException("登录失败");
        }
        if(StatusEnum.DISABLED.getCode().equals(sellerModel.getStatus())){
            throw new BusinessException(Errors.USER_HAS_DISABLED);
        }
        super.sessionUser(request,sellerModel);
        return ResponseVoUtil.success(sellerModel);
    }

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "卖家注册",notes = "卖家注册")
    @PostMapping("/insert")
    public ResponseVO<SellerModel> insert(@RequestBody @Valid SellerInsertRequestBean bean, BindingResult bindingResult, HttpServletRequest request){
        if(bindingResult.hasErrors()){
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        SellerModel sellerModel = convertSellerModel(bean);
        sellerModel = sellerService.insert(sellerModel);
        return ResponseVoUtil.success(sellerModel);
    }

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "校验",notes = "校验")
    @GetMapping("/check")
    public ResponseVO<SellerModel> checkOpenid(String openId, HttpServletRequest request){
        if(openId == null){
            return null;
        }
        SellerModel sellerModel = sellerService.get(openId);
        if(sellerModel != null){
            throw new BusinessException("用户已存在");
        }
        return ResponseVoUtil.success();
    }

    @ApiOperation(value = "查看seller列表根据店铺id",notes = "查看seller列表根据店铺id")
    @GetMapping("/list")
    public ResponseVO<List<SellerModel>> list(HttpServletRequest request){
        List<SellerModel> sellerModel = sellerService.listAllByShopId(getShopId(request));
        return ResponseVoUtil.success(sellerModel);
    }

    @ApiOperation(value = "修改seller状态",notes = "修改seller状态")
    @PostMapping("/updateStatus")
    public ResponseVO<SellerModel> update(@RequestBody StatusUpdateBean bean, HttpServletRequest request){
        // TODO 权限
        SellerModel sellerModel = new SellerModel();
        sellerModel.setId(bean.getId());
        sellerModel.setStatus(bean.getStatus());
        sellerModel = sellerService.updateStatus(sellerModel);
        return ResponseVoUtil.success(sellerModel);
    }

    private Integer getShopId(HttpServletRequest request){
        UserModel userModel = super.getSessionUser(request);
        if(userModel.getShopId() == null){
            throw new BusinessException("您还没有商铺");
        }
        return userModel.getShopId();
    }


    private SellerModel convertSellerModel(SellerInsertRequestBean bean){
        SellerModel sellerModel = new SellerModel();
        BeanUtils.copyProperties(bean,sellerModel);
        return sellerModel;
    }
}
